#include "Rp2VertexProgram.h"
#include "Rp2Catalog.h"

using namespace Rp2;

RP2_IMPLEMENT_RTTI(Rp2, VertexProgram, Program);
RP2_IMPLEMENT_DEFAULT_NAME_ID(VertexProgram, Program);
//---------------------------------------------------------------------------------------------------
VertexProgram::VertexProgram()
{
}
//---------------------------------------------------------------------------------------------------
VertexProgram::VertexProgram(const std::string& rkProgramName)
{
	// Try to compile Cg file
	std::string  kFilename = rkProgramName + std::string(".cg");
    const char* acDecorated = System::GetPath(kFilename.c_str(),
        System::SM_READ);
    if (acDecorated)
    {
		Program::CompileCg(std::string(acDecorated));
	} 

	// Try to load compiled program 
	kFilename = std::string("v_") + rkProgramName + 
		std::string(".") + RendererType;// + std::string(".wmsp");
	acDecorated = System::GetPath(kFilename.c_str(), System::SM_READ);
	bool bLoaded = Program::Load(std::string(acDecorated), this);
	assert(bLoaded);
	(void)bLoaded; // avoid warning in release builds
	SetName(rkProgramName.c_str());

    // Verify that the vertex program does not require more resources than the
    // renderer can support.
    assert(m_kInputAttributes.GetMaxColors() <= Renderer::GetMaxColors());
    assert(m_kInputAttributes.GetMaxTCoords() <= Renderer::GetMaxTCoords());
    assert(m_kOutputAttributes.GetMaxColors() <= Renderer::GetMaxColors());
    assert(m_kOutputAttributes.GetMaxTCoords() <= Renderer::GetMaxTCoords());
    assert((int)m_kSamplerInformation.size() <= Renderer::GetMaxVShaderImages());	

	// Insert new vertex program into catalog.
	Catalog<VertexProgram>::GetActive()->Insert(this);
}
//---------------------------------------------------------------------------------------------------
VertexProgram::~VertexProgram()
{
	Catalog<VertexProgram>::RemoveAll(this);
}
//---------------------------------------------------------------------------------------------------
VertexProgram* VertexProgram::Load(const std::string& rkProgramName)
{
	return new VertexProgram(rkProgramName);
}
//---------------------------------------------------------------------------------------------------